local js_lirang = fk.CreateSkill {
  name = "js__lirang",
}

js_lirang:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if target ~= player then
      return player:hasSkill(self) and target.phase == Player.Draw and #player:getCardIds("he") > 1 and
          player:usedSkillTimes(self.name, Player.HistoryRound) == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToCards(player,
      {
        min_num = 2,
        max_num = 2,
        include_equip = true,
        skill_name = self.name,
        cancelable = true,
        pattern = ".",
        prompt = "#js__lirang-invoke::" .. target.id
      })
    if #cards == 2 then
      event:setCostData(self,{tos = { target.id }, cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "js__lirang-round", target.id)
    room:moveCardTo(event:getCostData(self).cards, Card.PlayerHand, target, fk.ReasonGive, self.name, nil, true, player)
  end,
})
js_lirang:addEffect(fk.EventPhaseEnd, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if target ~= player then
      return target.phase == Player.Discard and player:usedSkillTimes(self.name, Player.HistoryTurn) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.from == target.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(cards, info.cardId)
          end
        end
      end
    end, Player.HistoryPhase)
    cards = table.filter(cards, function(id) return table.contains(room.discard_pile, id) end)
    if #cards == 0 then return end
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player)
  end,
})
return js_lirang
